import os
import time
import random

import ddddocr
import rsa
import binascii
import base64
import cv2

DIR_PATH = os.path.dirname(os.path.abspath(__file__))


def get_password(message, e='10001'):
    """
    RSA加密用户密码
    e 是偏移量 ，RSA默认的指数（65537的二进制就是10001）
    binascii.b2a_hex(crypto).decode() 把加密得到的二进制结果转为16进制字符串
    """
    n = '00C1E3934D1614465B33053E7F48EE4EC87B14B95EF88947713D25EECBFF7E74C7977D02DC1D9451F79DD5D1C10C29ACB6A9B4D6FB7D0A0279B6719E1772565F09AF627715919221AEF91899CAE08C0D686D748B20A3603BE2318CA6BC2B59706592A9219D0BF05C9F65023A21D2330807252AE0066D59CEEFA5F2748EA80BAB81'

    pub_key = rsa.PublicKey(int(n, 16), int(e, 16))
    crypto = rsa.encrypt(message.encode('utf8'), pub_key)
    return binascii.b2a_hex(crypto).decode().upper()  # base64.b64encode(crypto)


def download_img(session, name, url):
    """下载验证码图片"""
    resp = session.get(url)
    pic_dir = os.path.join(DIR_PATH, 'pic')
    if not os.path.exists(pic_dir):
        os.mkdir(pic_dir)
    with open(os.path.join(pic_dir, name), mode="wb") as f:
        f.write(resp.content)


def trun_back(path):
    # python画图的包pillow,  pip install pillow
    from PIL import Image  # Image 就是python的canvas
    old_img = Image.open(os.path.join(DIR_PATH, 'pic', path))
    # 创建一张新图
    new_img = Image.new("RGB", (260, 160))
    Ut = [39, 38, 48, 49, 41, 40, 46, 47, 35, 34, 50, 51, 33, 32, 28, 29, 27, 26, 36, 37, 31, 30, 44, 45, 43, 42, 12,
          13, 23, 22, 14, 15, 21, 20, 8, 9, 25, 24, 6, 7, 3, 2, 0, 1, 11, 10, 4, 5, 19, 18, 16, 17]
    r = 160
    a = r // 2  # 整数

    for _ in range(52):
        c = Ut[_] % 26 * 12 + 1
        if 25 < Ut[_]:
            u = a
        else:
            u = 0

        # 获取一个区域, (x1, y1, x2, y2)
        l = old_img.crop((c, u, c + 10, u + a))

        x1 = _ % 26 * 10
        if 25 < _:
            y1 = a
        else:
            y1 = 0
        new_img.paste(l, (x1, y1))

    new_img.save(os.path.join(DIR_PATH, 'pic', f"new_{path}"))


def get_x():
    slide = ddddocr.DdddOcr(det=False, ocr=False, show_ad=False)
    with open(os.path.join(DIR_PATH, 'pic', "new_bg.jpg"), 'rb') as f:
        target_bytes = f.read()
    with open(os.path.join(DIR_PATH, 'pic', "new_fullbg.jpg"), 'rb') as f:
        background_bytes = f.read()

    res = slide.slide_comparison(target_bytes, background_bytes)
    print(res)
    return res['target'][0]



# 三代的极验轨迹..
def __ease_out_expo(sep):
    if sep == 1:
        return 1
    else:
        return 1 - pow(2, -10 * sep)


def get_slide_track(distance):
    import random
    """
    根据滑动距离生成滑动轨迹
    :param distance: 需要滑动的距离
    :return: 滑动轨迹<type 'list'>: [[x,y,t], ...]
        x: 已滑动的横向距离
        y: 已滑动的纵向距离, 除起点外, 均为0
        t: 滑动过程消耗的时间, 单位: 毫秒
    """

    if not isinstance(distance, int) or distance < 0:
        raise ValueError(f"distance类型必须是大于等于0的整数: distance: {distance}, type: {type(distance)}")
    # 初始化轨迹列表
    slide_track = [
        [random.randint(-50, -10), random.randint(-50, -10), 0],
        [0, 0, 0],
    ]
    # 共记录count次滑块位置信息
    count = 30 + int(distance / 2)
    # 初始化滑动时间
    t = random.randint(50, 100)
    # 记录上一次滑动的距离
    _x = 0
    _y = 0
    for i in range(count):
        # 已滑动的横向距离
        x = round(__ease_out_expo(i / count) * distance)
        # 滑动过程消耗的时间
        t += random.randint(10, 20)
        if x == _x:
            continue
        slide_track.append([x, _y, t])
        _x = x
    slide_track.append([distance, 0, t])

    return slide_track, t


def get_track():
    gjArray = [
        ['move', 717, 147, 0, 'pointermove'],
        ['move', 716, 147, 1, 'mousemove'],
        ['move', 718, 147, 6, 'pointermove'],
        ['move', 720, 148, 16, 'pointermove'],
        ['move', 722, 151, 27, 'pointermove'],
        ['move', 722, 150, 28, 'mousemove'],
        ['move', 726, 151, 31, 'pointermove'],
        ['move', 725, 151, 32, 'mousemove'],
        ['move', 734, 152, 38, 'pointermove'],
        ['move', 733, 152, 39, 'mousemove'],
        ['move', 737, 152, 47, 'pointermove'],
        ['move', 738, 152, 55, 'pointermove'],
        ['move', 741, 152, 247, 'pointermove'],
        ['move', 744, 153, 254, 'pointermove'],
        ['move', 752, 156, 263, 'pointermove'],
        ['move', 761, 157, 270, 'pointermove'],
        ['move', 771, 157, 280, 'pointermove'],
        ['move', 782, 157, 286, 'pointermove'],
        ['move', 781, 157, 287, 'mousemove'],
        ['move', 794, 157, 296, 'pointermove'],
        ['move', 802, 157, 302, 'pointermove'],
        ['move', 801, 157, 303, 'mousemove'],
        ['move', 809, 156, 311, 'pointermove'],
        ['move', 816, 154, 318, 'pointermove'],
        ['move', 816, 153, 319, 'mousemove'],
        ['move', 822, 153, 325, 'pointermove'],
        ['move', 821, 153, 326, 'mousemove'],
        ['move', 827, 151, 333, 'pointermove'],
        ['move', 830, 150, 342, 'pointermove'],
        ['move', 834, 147, 349, 'pointermove'],
        ['move', 837, 145, 358, 'pointermove'],
        ['move', 838, 143, 365, 'pointermove'],
        ['move', 838, 143, 374, 'pointermove'],
        ['move', 838, 141, 381, 'pointermove'],
        ['move', 838, 140, 389, 'pointermove'],
        ['move', 838, 139, 397, 'pointermove'],
        ['move', 838, 136, 406, 'pointermove'],
        ['move', 838, 135, 413, 'pointermove'],
        ['move', 838, 135, 453, 'pointermove'],
        ['move', 838, 134, 454, 'mousemove'],
        ['move', 840, 135, 462, 'pointermove'],
        ['move', 842, 135, 478, 'pointermove'],
        ['move', 846, 135, 485, 'pointermove'],
        ['move', 853, 135, 494, 'pointermove'],
        ['move', 861, 135, 502, 'pointermove'],
        ['move', 871, 135, 510, 'pointermove'],
        ['move', 885, 135, 517, 'pointermove'],
        ['move', 898, 135, 526, 'pointermove'],
        ['move', 916, 135, 534, 'pointermove'],
        ['move', 939, 137, 542, 'pointermove'],
        ['move', 965, 138, 550, 'pointermove'],
        ['move', 989, 138, 557, 'pointermove'],
        ['move', 1014, 139, 565, 'pointermove'],
        ['move', 1034, 141, 572, 'pointermove'],
        ['move', 1054, 143, 580, 'pointermove'],
        ['move', 1077, 145, 588, 'pointermove'],
        ['move', 1098, 147, 596, 'pointermove'],
        ['move', 1111, 150, 604, 'pointermove'],
        ['move', 1121, 152, 612, 'pointermove'],
        ['move', 1126, 153, 620, 'pointermove'],
        ['move', 1128, 154, 628, 'pointermove'],
        ['move', 1129, 155, 644, 'pointermove'],
        ['move', 1129, 155, 652, 'pointermove'],
        ['move', 1129, 157, 661, 'pointermove'],
        ['move', 1129, 161, 677, 'pointermove'],
        ['move', 1129, 165, 685, 'pointermove'],
        ['move', 1128, 165, 686, 'mousemove'],
        ['move', 1129, 171, 694, 'pointermove'],
        ['move', 1129, 178, 701, 'pointermove'],
        ['move', 1129, 183, 710, 'pointermove'],
        ['move', 1130, 192, 717, 'pointermove'],
        ['move', 1130, 192, 718, 'mousemove'],
        ['move', 1132, 202, 725, 'pointermove'],
        ['move', 1132, 201, 726, 'mousemove'],
        ['move', 1135, 213, 733, 'pointermove'],
        ['move', 1138, 223, 741, 'pointermove'],
        ['move', 1138, 222, 742, 'mousemove'],
        ['move', 1140, 231, 749, 'pointermove'],
        ['move', 1141, 239, 757, 'pointermove'],
        ['move', 1141, 247, 765, 'pointermove'],
        ['move', 1139, 256, 773, 'pointermove'],
        ['move', 1139, 256, 774, 'mousemove'],
        ['move', 1134, 265, 781, 'pointermove'],
        ['move', 1126, 274, 790, 'pointermove'],
        ['move', 1118, 282, 797, 'pointermove'],
        ['move', 1109, 289, 805, 'pointermove'],
        ['move', 1108, 289, 806, 'mousemove'],
        ['move', 1100, 298, 813, 'pointermove'],
        ['move', 1091, 306, 821, 'pointermove'],
        ['move', 1082, 312, 829, 'pointermove'],
        ['move', 1071, 319, 837, 'pointermove'],
        ['move', 1061, 324, 845, 'pointermove'],
        ['move', 1051, 327, 853, 'pointermove'],
        ['move', 1051, 326, 854, 'mousemove'],
        ['move', 1042, 330, 861, 'pointermove'],
        ['move', 1041, 329, 862, 'mousemove'],
        ['move', 1030, 333, 870, 'pointermove'],
        ['move', 1021, 335, 878, 'pointermove'],
        ['move', 1011, 339, 885, 'pointermove'],
        ['move', 1011, 338, 886, 'mousemove'],
        ['move', 1002, 342, 894, 'pointermove'],
        ['move', 992, 343, 901, 'pointermove'],
        ['move', 992, 342, 902, 'mousemove'],
        ['move', 980, 347, 912, 'pointermove'],
        ['move', 980, 347, 913, 'mousemove'],
        ['move', 967, 349, 917, 'pointermove'],
        ['move', 957, 351, 925, 'pointermove'],
        ['move', 943, 354, 933, 'pointermove'],
        ['move', 943, 353, 934, 'mousemove'],
        ['move', 931, 357, 941, 'pointermove'],
        ['move', 922, 360, 949, 'pointermove'],
        ['move', 910, 361, 956, 'pointermove'],
        ['move', 902, 363, 964, 'pointermove'],
        ['move', 894, 364, 972, 'pointermove'],
        ['move', 885, 364, 980, 'pointermove'],
        ['move', 877, 364, 988, 'pointermove'],
        ['move', 871, 364, 996, 'pointermove'],
        ['move', 866, 364, 1004, 'pointermove'],
        ['move', 862, 364, 1012, 'pointermove'],
        ['move', 856, 364, 1021, 'pointermove'],
        ['move', 850, 363, 1029, 'pointermove'],
        ['move', 842, 360, 1038, 'pointermove'],
        ['move', 835, 359, 1045, 'pointermove'],
        ['move', 830, 356, 1053, 'pointermove'],
        ['move', 824, 355, 1061, 'pointermove'],
        ['move', 824, 355, 1062, 'mousemove'],
        ['move', 818, 354, 1070, 'pointermove'],
        ['move', 814, 351, 1078, 'pointermove'],
        ['move', 810, 347, 1085, 'pointermove'],
        ['move', 806, 345, 1093, 'pointermove'],
        ['move', 802, 342, 1101, 'pointermove'],
        ['move', 797, 340, 1110, 'pointermove'],
        ['move', 796, 338, 1119, 'pointermove'],
        ['move', 794, 335, 1125, 'pointermove'],
        ['move', 792, 332, 1133, 'pointermove'],
        ['move', 790, 329, 1141, 'pointermove'],
        ['move', 789, 325, 1149, 'pointermove'],
        ['move', 786, 323, 1157, 'pointermove'],
        ['move', 785, 321, 1165, 'pointermove'],
        ['move', 783, 319, 1173, 'pointermove'],
        ['move', 782, 319, 1181, 'pointermove'],
        ['move', 780, 317, 1189, 'pointermove'],
        ['move', 779, 316, 1197, 'pointermove'],
        ['move', 778, 316, 1213, 'pointermove'],
        ['move', 778, 315, 1229, 'pointermove'],
        ['down', 778, 315, 1512, 'pointerdown'],
        ['focus', 1513],
        ['up', 778, 315, 1597, 'pointerup']
    ]
    t = int(time.time() * 100)
    ranomx = random.randint(-10, 10)
    ranomy = random.randint(-20, 20)
    track = [[i[0], i[1] + ranomx, i[2] + ranomy, i[3] + t, i[4]] if len(i) == 5 else [i[0], i[1] + t] for i in gjArray]
    return track


if __name__ == '__main__':
    # a = get_password('c0caf3b942a8e17e')
    # a = get_password('c0caf3b942a8e17e')
    # print(a)

    # print(get_x())
    print(get_track())
